package com.jgzx.utils;

import cn.hutool.core.util.ObjectUtil;
import com.jgzx.constant.CodeSerialTypeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description
 * @Author chenzy
 * @Date 2021/9/14 11:20
 **/
public class FlowCodeUtils {


    /**
     * 根据传入的code计算出下一个code,如果返回空即代表无编码可编
     * @param code 编码
     * @return 下一个编码
     */
    public static String getNextCode(String code) {
        int length = code.length();
        //判断类型，如果是数字和字母直接加即可
        if (code.matches("[9zZ]+")) {
            code = null;
        } else if(code.matches("[0-9a-zA-Z]+")) {
            code = getNextLetter(code);
        } else {
            code = null;
        }
        return code;
    }

    /**
     * 字母数字加法
     * @return
     */
    private static String getNextLetter(String code) {
        char [] tempChar = code.toCharArray();
        for (int i = tempChar.length-1; i >= 0; i--) {
            if (tempChar[i] == 'Z') {
                tempChar[i]='A';
            } else if (tempChar[i] == 'z') {
                tempChar[i]='a';
            } else if (tempChar[i] == '9') {
                tempChar[i]='0';
            } else {
                tempChar[i]=(char)(tempChar[i]+1);
                break;
            }
        }
        return String.valueOf(tempChar);
    }
    public static String getSecondNodeNextSerial(String code, String enum_value, int length) {
        if(ObjectUtil.isNull(enum_value)){
            return getNextCode(code);
        }
        if(ObjectUtil.isNull(CodeSerialTypeEnum.getCodeSerialTypeByValue(enum_value))){
            return getNextCode(code);
        }

        //长度检查、补齐
        code = ObjectUtil.defaultIfBlank(code,"");
        if(code.length()>length){
            code = "";
        }
        if(code.length() < length){
            String padStr = "";
            if(CodeSerialTypeEnum.D.value().equals(enum_value) || CodeSerialTypeEnum.DL.value().equals(enum_value)){
                padStr = "0";
            }if(CodeSerialTypeEnum.L.value().equals(enum_value) || CodeSerialTypeEnum.LD.value().equals(enum_value)){
                padStr = "A";
            }
            code = StringUtils.leftPad(code, length, padStr);
            if(ObjectUtil.isEmpty(code)){
                return code;
            }
        }

        //是否结尾
        String regex = "";
        if(CodeSerialTypeEnum.D.value().equals(enum_value) || CodeSerialTypeEnum.LD.value().equals(enum_value)){
            regex = "[9]+";
        }else if(CodeSerialTypeEnum.L.value().equals(enum_value) || CodeSerialTypeEnum.DL.value().equals(enum_value)){
            regex = "[Z]+";
        }
        if (code.matches(regex)) {
            return null;
        }
        return getNextLetter(code, enum_value);
    }

    private static String getNextLetter(String code, String enum_value) {
        char [] tempChar = code.toCharArray();
        for (int i = tempChar.length-1; i >= 0; i--) {
            if (tempChar[i] == 'Z') {
                tempChar[i]='A';
                //字母加数字 A-Z,0-9
                if(CodeSerialTypeEnum.LD.value().equals(enum_value)){
                    tempChar[i]='0';
                    break;
                }
            } else if (tempChar[i] == 'z') {
                tempChar[i]='a';
            } else if (tempChar[i] == '9') {
                tempChar[i]='0';
                //数字加字母 0-9,A-Z
                if(CodeSerialTypeEnum.DL.value().equals(enum_value)){
                    tempChar[i]='A';
                    break;
                }
            } else {
                tempChar[i]=(char)(tempChar[i]+1);
                break;
            }
        }
        return String.valueOf(tempChar);
    }

    public static void main(String[] args) {
        System.out.println(getSecondNodeNextSerial(null,CodeSerialTypeEnum.DL.value(), 3));
    }
}
